{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "from os.path import join\n",
    "import re\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_paths = '/home/njuciairs/wangshuai/competitions/finacial_models_round2/*/version*'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "dirs = glob(model_paths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross1/version1'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dirp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version9/score0.919583_epoch8_time20191112111410']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version8/score0.907922_epoch2_time20191112071221']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version1/score0.922166_epoch3_time20191111030235']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version2/score0.926959_epoch6_time20191111082927']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version3/score0.915481_epoch5_time20191111115139']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version5/score0.917137_epoch6_time20191111221356']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version6/score0.921900_epoch6_time20191112014343']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version7/score0.932367_epoch5_time20191112045514']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross_roberta/version4/score0.936144_epoch7_time20191111184620']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version9/score0.955598_epoch9_time20191109111611']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version8/score0.925111_epoch6_time20191109090301']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version1/score0.935708_epoch5_time20191108200756']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version2/score0.965252_epoch9_time20191108220927']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version3/score0.951920_epoch9_time20191108233913']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version5/score0.949487_epoch6_time20191109033451']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version6/score0.943984_epoch4_time20191109053914']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version7/score0.953871_epoch6_time20191109074801']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_goodremove_on_round1data/version4/score0.952887_epoch5_time20191109013920']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/senti_entity_inplace_hit_full_v1/version1/score0.942072_epoch2_time20191114210551']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross1/version9/score0.916217_epoch3_time20191108050017']\n",
      "['/home/njuciairs/wangshuai/competitions/finacial_models_round2/multi_class_cross1/version8/score0.933245_epoch9_time20191108003930']\n",
      "[]\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-37-b12fd78dbf59>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpaths\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m     \u001b[0;32massert\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdelete_paths\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpaths\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m     \u001b[0;34m[\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0md\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdelete_paths\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "for dirp in dirs:\n",
    "    paths = glob(join(dirp,'*epoch*'))\n",
    "    delete_paths = sorted(paths,key = (lambda x:int(re.findall('epoch(\\d)',x)[0])))[:-1]\n",
    "\n",
    "    print(paths)\n",
    "    assert len(delete_paths)+1 == len(paths)\n",
    "    [os.remove(d) for d in delete_paths]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
